home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / ampeg43 / source / decodsrc / common.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  19.6 KB  |  533 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.h
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 708-538-5671, e-mail: pan@ukraine.corp.mot.com        *
  10.  *                                                                    *
  11.  * VERSION 4.3                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Reorganized & renamed all ".h" files  *
  17.  *                              into "common.h" and "encoder.h".      *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added additional type definitions for *
  20.  *                              AIFF, double/SANE and "bitstream.c".  *
  21.  *                              Added function prototypes for more    *
  22.  *                              rigorous type checking.               *
  23.  * 27jun91  dpwe (Aware)        Added "alloc_*" defs & prototypes     *
  24.  *                              Defined new struct 'frame_params'.    *
  25.  *                              Changed info.stereo to info.mode_ext  *
  26.  *                              #define constants for mode types      *
  27.  *                              Prototype arguments if PROTO_ARGS     *
  28.  * 5/28/91  Earle Jennings      added MS_DOS definition               *
  29.  *                              MsDos function prototype declarations *
  30.  * 7/10/91  Earle Jennings      added FLOAT definition as double      *
  31.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  32.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  33.  *                              important fixes involved changing     *
  34.  *                              16-bit ints to long or unsigned in    *
  35.  *                              bit alloc routines for quant of 65535 *
  36.  *                              and passing proper function args.     *
  37.  *                              Removed "Other Joint Stereo" option   *
  38.  *                              and made bitrate be total channel     *
  39.  *                              bitrate, irrespective of the mode.    *
  40.  *                              Fixed many small bugs & reorganized.  *
  41.  *                              Modified some function prototypes.    *
  42.  *                              Changed BUFFER_SIZE back to 4096.     *
  43.  * 7/27/92  Michael Li          (re-)Ported to MS-DOS                 *
  44.  * 7/27/92  Masahiro Iwadare    Ported to Convex                      *
  45.  * 8/07/92  mc@tv.tek.com                                             *
  46.  * 8/10/92  Amit Gulati         Ported to the AIX Platform (RS6000)   *
  47.  *                              AIFF string constants redefined       *
  48.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  49.  *         Daniel Lauzon, and                                         *
  50.  *         Bill Truerniet                                             *
  51.  *--------------------------------------------------------------------*
  52.  * 4/23/92  J. Pineda           Added code for Layer III.             *
  53.  * 11/9/92  Amit Gulati         Added defines for layerIII stereo     *
  54.  *                              modes.                                *
  55.  *  8/24/93 Masahiro Iwadare    Included IS modification in Layer III.*
  56.  *                              Changed for 1 pass decoding.          *
  57.  *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
  58.  *--------------------------------------------------------------------*
  59.  * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
  60.  *--------------------------------------------------------------------*
  61.  *  7/14/94 Juergen Koller      Fix for HPUX an IRIX in AIFF-Strings  *
  62.  **********************************************************************/
  63.  
  64. /***********************************************************************
  65. *
  66. *  Global Conditional Compile Switches
  67. *
  68. ***********************************************************************/
  69.  
  70. /* #define      UNIX            /* Unix conditional compile switch */
  71. /* #define      MACINTOSH       /* Macintosh conditional compile switch */
  72. /* #define      MS_DOS          /* IBM PC conditional compile switch */
  73. /* #define      MSC60           /* Compiled for MS_DOS with MSC v6.0 */
  74. /* #define      AIX             /* AIX conditional compile switch    */
  75. /* #define      CONVEX          /* CONVEX conditional compile switch */
  76.  
  77. #if defined(MSC60) 
  78. #ifndef MS_DOS
  79. #define MS_DOS
  80. #endif
  81. #ifndef PROTO_ARGS
  82. #define PROTO_ARGS
  83. #endif
  84. #endif
  85.  
  86. #ifdef  UNIX
  87. #define         TABLES_PATH     "tables"  /* to find data files */
  88. /* name of environment variable holding path of table files */
  89. #define         MPEGTABENV      "MPEGTABLES"
  90. #define         PATH_SEPARATOR  "/"        /* how to build paths */
  91. #endif  /* UNIX */
  92.  
  93. #ifdef  MACINTOSH
  94. /* #define      TABLES_PATH ":tables:"  /* where to find data files */
  95. #endif  /* MACINTOSH */
  96.  
  97. /* 
  98.  * Don't define FAR to far unless you're willing to clean up the 
  99.  * prototypes
  100.  */
  101. #define FAR /*far*/
  102.  
  103. #ifdef __STDC__
  104. #ifndef PROTO_ARGS
  105. #define PROTO_ARGS
  106. #endif
  107. #endif
  108.  
  109. #ifdef CONVEX
  110. #define SEEK_SET        0
  111. #define SEEK_CUR        1
  112. #define SEEK_END        2
  113. #endif
  114.  
  115. /* MS_DOS and VMS do not define TABLES_PATH, so OpenTableFile will default
  116.    to finding the data files in the default directory */
  117.  
  118. /***********************************************************************
  119. *
  120. *  Global Include Files
  121. *
  122. ***********************************************************************/
  123.  
  124. #include        <stdio.h>
  125. #include        <string.h>
  126. #include        <math.h>
  127.  
  128. #ifdef  UNIX
  129. #include        <unistd.h>
  130. #endif  /* UNIX */
  131.  
  132. #ifdef  MACINTOSH
  133. #include        <stdlib.h>
  134. #include        <console.h>
  135. #endif  /* MACINTOSH */
  136.  
  137. #ifdef  MS_DOS
  138. #include        <stdlib.h>
  139. #ifdef MSC60
  140. #include        <memory.h>
  141. #else
  142. #include        <alloc.h>
  143. #include        <mem.h>
  144. #endif  /* MSC60 */
  145. #endif  /* MS_DOS */
  146.  
  147. /***********************************************************************
  148. *
  149. *  Global Definitions
  150. *
  151. ***********************************************************************/
  152.  
  153. /* General Definitions */
  154.  
  155. #ifdef  MS_DOS
  156. #define         FLOAT                   double
  157. #else
  158. #define         FLOAT                   float
  159. #endif
  160.  
  161. #define         FALSE                   0
  162. #define         TRUE                    1
  163. #define         NULL_CHAR               '\0'
  164.  
  165. #define         MAX_U_32_NUM            0xFFFFFFFF
  166. #define         PI                      3.14159265358979
  167. #define         PI4                     PI/4
  168. #define         PI64                    PI/64
  169. #define         LN_TO_LOG10             0.2302585093
  170.  
  171. #define         VOL_REF_NUM             0
  172. #define         MPEG_AUDIO_ID           1
  173. #define         MAC_WINDOW_SIZE         24
  174.  
  175. #define         MONO                    1
  176. #define         STEREO                  2
  177. #define         BITS_IN_A_BYTE          8
  178. #define         WORD                    16
  179. #define         MAX_NAME_SIZE           81
  180. #define         SBLIMIT                 32
  181. #define         SSLIMIT                 18
  182. #define         FFT_SIZE                1024
  183. #define         HAN_SIZE                512
  184. #define         SCALE_BLOCK             12
  185. #define         SCALE_RANGE             64
  186. #define         SCALE                   32768
  187. #define         CRC16_POLYNOMIAL        0x8005
  188.  
  189. /* MPEG Header Definitions - Mode Values */
  190.  
  191. #define         MPG_MD_STEREO           0
  192. #define         MPG_MD_JOINT_STEREO     1
  193. #define         MPG_MD_DUAL_CHANNEL     2
  194. #define         MPG_MD_MONO             3
  195.  
  196. /* Mode Extention */
  197.  
  198. #define         MPG_MD_LR_LR             0
  199. #define         MPG_MD_LR_I              1
  200. #define         MPG_MD_MS_LR             2
  201. #define         MPG_MD_MS_I              3
  202.  
  203. /* AIFF Definitions */
  204.  
  205. /* 
  206.  * Note:  The value of a multi-character constant
  207.  *        is implementation-defined.
  208.  */
  209. #if !defined(MS_DOS) && !defined(AIX) && !defined(__hpux) && !defined(sgi)
  210. #define         IFF_LONG
  211. #define         IFF_ID_FORM             'FORM'
  212. #define         IFF_ID_AIFF             'AIFF'
  213. #define         IFF_ID_COMM             'COMM'
  214. #define         IFF_ID_SSND             'SSND'
  215. #define         IFF_ID_MPEG             'MPEG'
  216. #else
  217. #define         IFF_ID_FORM             "FORM"
  218. #define         IFF_ID_AIFF             "AIFF"
  219. #define         IFF_ID_COMM             "COMM"
  220. #define         IFF_ID_SSND             "SSND"
  221. #define         IFF_ID_MPEG             "MPEG"
  222. #endif
  223.  
  224. /* "bit_stream.h" Definitions */
  225.  
  226. #define         MINIMUM         4    /* Minimum size of the buffer in bytes */
  227. #define         MAX_LENGTH      32   /* Maximum length of word written or
  228.                                         read from bit stream */
  229. #define         READ_MODE       0
  230. #define         WRITE_MODE      1
  231. #define         ALIGNING        8
  232. #define         BINARY          0
  233. #define         ASCII           1
  234. #define         BS_FORMAT       ASCII /* BINARY or ASCII = 2x bytes */
  235. #define         BUFFER_SIZE     4096
  236.  
  237. #define         MIN(A, B)       ((A) < (B) ? (A) : (B))
  238. #define         MAX(A, B)       ((A) > (B) ? (A) : (B))
  239.  
  240. /***********************************************************************
  241. *
  242. *  Global Type Definitions
  243. *
  244. ***********************************************************************/
  245.  
  246. /* Structure for Reading Layer II Allocation Tables from File */
  247.  
  248. typedef struct {
  249.     unsigned int    steps;
  250.     unsigned int    bits;
  251.     unsigned int    group;
  252.     unsigned int    quant;
  253. } sb_alloc, *alloc_ptr;
  254.  
  255. typedef sb_alloc        al_table[SBLIMIT][16];
  256.  
  257. /* Header Information Structure */
  258.  
  259. typedef struct {
  260.     int version;
  261.     int lay;
  262.     int error_protection;
  263.     int bitrate_index;
  264.     int sampling_frequency;
  265.     int padding;
  266.     int extension;
  267.     int mode;
  268.     int mode_ext;
  269.     int copyright;
  270.     int original;
  271.     int emphasis;
  272. } layer, *the_layer;
  273.  
  274. /* Parent Structure Interpreting some Frame Parameters in Header */
  275.  
  276. typedef struct {
  277.     layer       *header;        /* raw header information */
  278.     int         actual_mode;    /* when writing IS, may forget if 0 chs */
  279.     al_table    *alloc;         /* bit allocation table read in */
  280.     int         tab_num;        /* number of table as loaded */
  281.     int         stereo;         /* 1 for mono, 2 for stereo */
  282.     int         jsbound;        /* first band of joint stereo coding */
  283.     int         sblimit;        /* total number of sub bands */
  284. } frame_params;
  285.  
  286. /* Double and SANE Floating Point Type Definitions */
  287.  
  288. typedef struct  IEEE_DBL_struct {
  289.     unsigned long   hi;
  290.     unsigned long   lo;
  291. } IEEE_DBL;
  292.  
  293. typedef struct  SANE_EXT_struct {
  294.     unsigned long   l1;
  295.     unsigned long   l2;
  296.     unsigned short  s1;
  297. } SANE_EXT;
  298.  
  299. /* AIFF Type Definitions */
  300.  
  301. typedef char    ID[4];
  302.  
  303. typedef struct  ChunkHeader_struct {
  304.     ID      ckID;
  305.     long    ckSize;
  306. } ChunkHeader;
  307.  
  308. typedef struct  Chunk_struct {
  309.     ID      ckID;
  310.     long    ckSize;
  311.     ID      formType;
  312. } Chunk;
  313.  
  314. typedef struct  CommonChunk_struct {
  315.     ID              ckID;
  316.     long            ckSize;
  317.     short           numChannels;
  318.     unsigned long   numSampleFrames;
  319.     short           sampleSize;
  320.     char            sampleRate[10];
  321. } CommonChunk;
  322.  
  323. typedef struct  SoundDataChunk_struct {
  324.     ID              ckID;
  325.     long            ckSize;
  326.     unsigned long   offset;
  327.     unsigned long   blockSize;
  328. } SoundDataChunk;
  329.  
  330. typedef struct  blockAlign_struct {
  331.     unsigned long   offset;
  332.     unsigned long   blockSize;
  333. } blockAlign;
  334.  
  335. typedef struct  IFF_AIFF_struct {
  336.     short           numChannels;
  337.     unsigned long   numSampleFrames;
  338.     short           sampleSize;
  339.     double          sampleRate;
  340.     unsigned long   sampleType;
  341.     blockAlign      blkAlgn;
  342. } IFF_AIFF;
  343.  
  344. /* "bit_stream.h" Type Definitions */
  345.  
  346. typedef struct  bit_stream_struc {
  347.     FILE        *pt;            /* pointer to bit stream device */
  348.     unsigned char *buf;         /* bit stream buffer */
  349.     int         buf_size;       /* size of buffer (in number of bytes) */
  350.     long        totbit;         /* bit counter of bit stream */
  351.     int         buf_byte_idx;   /* pointer to top byte in buffer */
  352.     int         buf_bit_idx;    /* pointer to top bit of top byte in buffer */
  353.     int         mode;           /* bit stream open in read or write mode */
  354.     int         eob;            /* end of buffer index */
  355.     int         eobs;           /* end of bit stream flag */
  356.     char        format;
  357.     
  358.     /* format of file in rd mode (BINARY/ASCII) */
  359. } Bit_stream_struc;
  360.  
  361. /* Layer III side information. */
  362.  
  363. typedef struct {
  364.     unsigned main_data_begin;
  365.     unsigned private_bits;
  366.     struct {
  367.         unsigned scfsi[4];
  368.         struct gr_info_s {
  369.             unsigned part2_3_length;
  370.             unsigned big_values;
  371.             unsigned global_gain;
  372.             unsigned scalefac_compress;
  373.             unsigned window_switching_flag;
  374.             unsigned block_type;
  375.             unsigned mixed_block_flag;
  376.             unsigned table_select[3];
  377.             unsigned subblock_gain[3];
  378.             unsigned region0_count;
  379.             unsigned region1_count;
  380.             unsigned preflag;
  381.             unsigned scalefac_scale;
  382.             unsigned count1table_select;
  383.             } gr[2];
  384.         } ch[2];
  385.     } III_side_info_t;
  386.  
  387. /* Layer III scale factors. */
  388.  
  389. typedef struct {
  390.     int l[23];            /* [cb] */
  391.     int s[3][13];         /* [window][cb] */
  392.     } III_scalefac_t[2];  /* [ch] */
  393.  
  394. /***********************************************************************
  395. *
  396. *  Global Variable External Declarations
  397. *
  398. ***********************************************************************/
  399.  
  400. extern char     *mode_names[4];
  401. extern char     *layer_names[3];
  402. extern double   s_freq[4];
  403. extern int      bitrate[3][15];
  404. extern double FAR multiple[64];
  405.  
  406. /***********************************************************************
  407. *
  408. *  Global Function Prototype Declarations
  409. *
  410. ***********************************************************************/
  411.  
  412. /* The following functions are in the file "common.c" */
  413.  
  414. #ifdef  PROTO_ARGS
  415. extern FILE           *OpenTableFile(char*);
  416. extern int            read_bit_alloc(int, al_table*);
  417. extern int            pick_table(frame_params*);
  418. extern int            js_bound(int, int);
  419. extern void           hdr_to_frps(frame_params*);
  420. extern void           WriteHdr(frame_params*, FILE*);
  421. extern void           WriteBitAlloc(unsigned int[2][SBLIMIT], frame_params*,
  422.                         FILE*);
  423. extern void           WriteScale(unsigned int[2][SBLIMIT],
  424.                         unsigned int[2][SBLIMIT], unsigned int[2][3][SBLIMIT],
  425.                         frame_params*, FILE*);
  426. extern void           WriteSamples(int, unsigned int FAR [SBLIMIT],
  427.                         unsigned int[SBLIMIT], frame_params*, FILE*);
  428. extern int            NumericQ(char*);
  429. extern int            BitrateIndex(int, int);
  430. extern int            SmpFrqIndex(long);
  431. extern int            memcheck(char*, int, int);
  432. extern void           FAR *mem_alloc(unsigned long, char*);
  433. extern void           mem_free(void**);
  434. extern void           double_to_extended(double*, char[10]);
  435. extern void           extended_to_double(char[10], double*);
  436. extern int            aiff_read_headers(FILE*, IFF_AIFF*);
  437. extern int            aiff_seek_to_sound_data(FILE*);
  438. extern int            aiff_write_headers(FILE*, IFF_AIFF*);
  439. extern int            refill_buffer(Bit_stream_struc*);
  440. extern void           empty_buffer(Bit_stream_struc*, int);
  441. extern void           open_bit_stream_w(Bit_stream_struc*, char*, int);
  442. extern void           open_bit_stream_r(Bit_stream_struc*, char*, int);
  443. extern void           close_bit_stream_r(Bit_stream_struc*);
  444. extern void           close_bit_stream_w(Bit_stream_struc*);
  445. extern void           alloc_buffer(Bit_stream_struc*, int);
  446. extern void           desalloc_buffer(Bit_stream_struc*);
  447. extern void           back_track_buffer(Bit_stream_struc*, int);
  448. extern unsigned int   get1bit(Bit_stream_struc*);
  449. extern void           put1bit(Bit_stream_struc*, int);
  450. extern unsigned long  look_ahead(Bit_stream_struc*, int);
  451. extern unsigned long  getbits(Bit_stream_struc*, int);
  452. extern void           putbits(Bit_stream_struc*, unsigned int, int);
  453. extern void           byte_ali_putbits(Bit_stream_struc*, unsigned int, int);
  454. extern unsigned long  byte_ali_getbits(Bit_stream_struc*, int);
  455. extern unsigned long  sstell(Bit_stream_struc*);
  456. extern int            end_bs(Bit_stream_struc*);
  457. extern int            seek_sync(Bit_stream_struc*, long, int);
  458. extern void           I_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  459.                         unsigned int*);
  460. extern void           II_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  461.                         unsigned int[2][SBLIMIT], unsigned int*);
  462. extern void           update_CRC(unsigned int, unsigned int, unsigned int*);
  463. extern void           read_absthr(FLOAT*, int);
  464. extern unsigned int   hget1bit(); /* MI */
  465. extern unsigned long  hgetbits(int);
  466. extern unsigned long  hsstell();
  467. extern void           hputbuf(unsigned int,int);
  468.  
  469.  
  470.  
  471. #ifdef  MACINTOSH
  472. extern void           set_mac_file_attr(char[MAX_NAME_SIZE], short, OsType,
  473.                         OsType);
  474. #endif
  475. #ifdef MS_DOS
  476. extern char           *new_ext(char *filename, char *extname); 
  477. #endif
  478.  
  479. #else
  480. extern FILE           *OpenTableFile();
  481. extern int            read_bit_alloc();
  482. extern int            pick_table();
  483. extern int            js_bound();
  484. extern void           hdr_to_frps();
  485. extern void           WriteHdr();
  486. extern void           WriteBitAlloc();
  487. extern void           WriteScale();
  488. extern void           WriteSamples();
  489. extern int            NumericQ();
  490. extern int            BitrateIndex();
  491. extern int            SmpFrqIndex();
  492. extern int            memcheck();
  493. extern void           FAR *mem_alloc();
  494. extern void           mem_free();
  495. extern void           double_to_extended();
  496. extern void           extended_to_double();
  497. extern int            aiff_read_headers();
  498. extern int            aiff_seek_to_sound_data();
  499. extern int            aiff_write_headers();
  500. extern int            refill_buffer();
  501. extern void           empty_buffer();
  502. extern void           open_bit_stream_w();
  503. extern void           open_bit_stream_r();
  504. extern void           close_bit_stream_r();
  505. extern void           close_bit_stream_w();
  506. extern void           alloc_buffer();
  507. extern void           desalloc_buffer();
  508. extern void           back_track_buffer();
  509. extern unsigned int   get1bit();
  510. extern void           put1bit();
  511. extern unsigned long  look_ahead();
  512. extern unsigned long  getbits();
  513. extern void           putbits();
  514. extern void           byte_ali_putbits();
  515. extern unsigned long  byte_ali_getbits();
  516. extern unsigned long  sstell();
  517. extern int            end_bs();
  518. extern int            seek_sync();
  519. extern void           I_CRC_calc();
  520. extern void           II_CRC_calc();
  521. extern void           update_CRC();
  522. extern void           read_absthr();
  523.  
  524. extern unsigned int   hget1bit();
  525. extern unsigned long  hgetbits();
  526. extern unsigned long  hsstell();
  527. extern void           hputbuf();
  528.  
  529. #ifdef MS_DOS
  530. extern char           *new_ext(); 
  531. #endif
  532. #endif
  533.